package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.pos.R;
import cn.mumzone.pos.presenter.ReserveWeekEntity;

/**
 * Created by qiuzhenhuan on 2019/7/6.
 */


public class ADA_ReserveWeek extends CommonAdapter<ReserveWeekEntity> {


    public ADA_ReserveWeek(Context context) {
        super(context);
    }

    private String mSelectedDate;
    private ViewHolder mPreHolder;

    public void setSelectedDate(String mSelectedDate) {
        this.mSelectedDate = mSelectedDate;
        mPreHolder = null;
    }


    @Override
    protected int itemLayoutId() {
        return R.layout.item_update_reserve_time;
    }

    @Override
    protected void convert(final ViewHolder holder, final ReserveWeekEntity item, int position) {

        final RadioButton mCurrentView = (RadioButton) holder.getConvertView();

        if (position < 2) {
            mCurrentView.setText(item.getWeek());
        } else {
            mCurrentView.setText(String.format("%s %s", item.getDate(), item.getWeek()));
        }

        if (mPreHolder == null && item.getDate().equals(mSelectedDate)) {
            mCurrentView.setChecked(true);
            mPreHolder = holder;
        } else {
            mCurrentView.setChecked(false);
        }

        // 点击事件
        mCurrentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 点击同一条目不做响应
                if (mPreHolder == holder) return;

                // 点击事件
                if (mListener != null) {
                    mListener.onItemSelected(item);
                }

                // 首次选择
                if (mPreHolder == null) {
                    RadioButton radioButton = (RadioButton) holder.getConvertView();
                    radioButton.setChecked(true);
                    mPreHolder = holder;
                    return;
                }

                // 当前点击条目
                RadioButton radioButton = (RadioButton) holder.getConvertView();
                radioButton.setChecked(true);

                // 重置之前条目
                RadioButton preButton = (RadioButton) mPreHolder.getConvertView();
                preButton.setChecked(false);

                mPreHolder = holder;

            }
        });
    }


    public interface OnItemSelectedListener {
        void onItemSelected(ReserveWeekEntity entity);
    }

    private OnItemSelectedListener mListener;

    public void setOnItemSelectedListener(OnItemSelectedListener mListener) {
        this.mListener = mListener;
    }

    /**
     * 获取当前选中的条目
     *
     * @return
     */
    public ReserveWeekEntity getSelectedWeek() {
        if (mPreHolder == null) return null;
        int itemPosition = mPreHolder.getAdapterPosition();
        return mDatas.get(itemPosition);
    }

}
